package com.tipfy.tmx;


class TiledObject
{
    /**
     * required, length > 0
     */
    public var name:String;

    /**
     * required, string
     */
    public var type:String;

    /**
     * required, nonNegativeInteger
     */
    public var x:Int;

    /**
     * required, nonNegativeInteger
     */
    public var y:Int;

    /**
     * nonNegativeInteger
     */
    public var width:Int;

    /**
     * nonNegativeInteger
     */
    public var height:Int;

    /**
     *
     */
    public var properties:TiledProperties;

    /**
     * Constructor.
     */
    public function new(xml:Xml)
    {
        // Attributes.
        this.name = TiledAttributes.getNonEmpty(xml, "name");
        this.type = TiledAttributes.get(xml, "type");
        this.x = TiledAttributes.getNonNegativeInt(xml, "x");
        this.y = TiledAttributes.getNonNegativeInt(xml, "y");
        this.width = TiledAttributes.getNonNegativeInt(xml, "width", null,
            false);
        this.height = TiledAttributes.getNonNegativeInt(xml, "height", null,
            false);

        // Child elements.
        for (elem in xml.elements())
        {
            switch (elem.nodeName)
            {
                case "properties":
                    this._loadProperties(elem);
                default:
                    throw "Invalid element <" + elem.nodeName +
                        "> in <object>";
            }
        }
    }

    /**
     * <properties>. minOccurs="0", maxOccurs="1"
     */
    private function _loadProperties(xml:Xml):Void
    {
        if (this.properties != null)
        {
            throw "Duplicated <properties> tag.";
        }

        this.properties = new TiledProperties(xml);
    }
}
